package com.ffapp.rainmonitorcustom.video;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.ElementListBean;
import com.ffapp.rainmonitorcustom.bean.ElementListData;
import com.ffapp.rainmonitorcustom.util.MyUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/***
 * 水质监控
 * **/
public class VideoInfoActivity extends BasisActivity {


    @BindView(R.id.img_bg)
    ImageView mImgBg;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    @BindView(R.id.rb_line_one)
    RadioButton rbLineOne;
    @BindView(R.id.rb_line_two)
    RadioButton rbLineTwo;
    @BindView(R.id.rb_line_three)
    RadioButton rbLineThree;
    @BindView(R.id.rb_line_four)
    RadioButton rbLineFour;
    private MyAdapterList mAdapter;
    private ElementListBean mListBean;


    @Override
    public void initViews() {
        // TODO Auto-generated method stub
        super.initViews();
        setContentView(R.layout.video_info_activity);


        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);

        setTitle("水质监控");
        setTitleLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_line_one:
                        params =0;
                        break;
                    case R.id.rb_line_two:
                        params = 1;
                        break;
                    case R.id.rb_line_three:
                        params = 2;
                        break;
                    case R.id.rb_line_four:
                        params = 3;
                        break;
                }
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.initData(savedInstanceState);
        String deviceCode = getIntent().getStringExtra("stcd");
        if (TextUtils.isEmpty(deviceCode)) {
            showToast("设备编号不存在");
            finish();
            return;
        }

        String imgBg = getIntent().getStringExtra("bg");
        if (!TextUtils.isEmpty(imgBg)) {
//            BasisApp.loadImg(mContext,Constants.URL_TEST_API + imgBg, mImgBg, R.drawable.video_img_bg_default);
            BasisApp.loadImg(mContext, MyUtils.getServiceAPI() + imgBg, mImgBg, R.drawable.video_img_bg_default);
        }

        String title = getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
           setTitle(""+title);
        }


        mListBean = new ElementListBean();
        getList(deviceCode);
    }


    private void getList(String deviceCode) {
        RequestParams params = new RequestParams();
        showProgress();
        params.put("deviceCode", deviceCode);
        HttpRestClient.get(Constants.URL_MONITOR_DATA, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        ElementListBean listBean = (ElementListBean) result;
                        mListBean = listBean;
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                        setListView();
                    }
                },
                0, ElementListBean.class);

    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }

    }


    private int params = 0;

    @OnClick(R.id.view_play)
    void viewplay() {
        if(TextUtils.isEmpty(mListBean.getVideoUrl(params))){
            showToast("视频地址不存在");
            return;
        }
        Bundle extras = new Bundle();
        extras.putSerializable("url", mListBean.getVideoUrl(params));
        ActivityTool.skipActivity(mContext, VideoDetailActivity.class, extras);
    }



    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }


        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.sensor_info, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {

            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getSensorBasicInfoList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_name)
            public TextView mTvName;
            @BindView(R.id.tv_flag)
            public TextView mTvFlag;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                Map<String, Object> stDeviceFactor = mListBean.getStDeviceFactor();
                ElementListData sensorInfoData = mListBean.getSensorBasicInfoList().get(position);
                mTvName.setText(sensorInfoData.getName()+" : ");
                if (stDeviceFactor != null) {
                    mTvFlag.setText("" + isEpty(stDeviceFactor.get(sensorInfoData.getFlag())) + "/" + sensorInfoData.getUnit());
                } else {
                    mTvFlag.setText(" 0 " + sensorInfoData.getUnit());
                }
            }

            private Object isEpty(Object data) {
                if (data == null) {
                    return "0";
                }
                return data;
            }

        }
    }

}
